package cn.lili.modules.goods.sku;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONUtil;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.mybatis.util.SceneHelp;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author paulG
 * @since 2022/5/20
 **/
@Component
public class GoodsSkuBuilder {


    /**
     * 构建商品sku
     *
     * @param goods   商品
     * @param skuInfo sku信息列表
     * @return 商品sku
     */
    public static GoodsSku build(Goods goods, Map<String, Object> skuInfo) {
        GoodsSku goodsSku = new GoodsSku(goods);
        builderSingle(goodsSku, skuInfo);
        return goodsSku;
    }

    /**
     * 批量构建商品sku
     *
     * @param goods   商品
     * @param skuList 商品sku信息
     * @return 商品sku
     */
    public static List<GoodsSku> buildBatch(Goods goods, List<Map<String, Object>> skuList) {
        return builderBatch(goods, skuList);
    }


    public static void builderSingle(GoodsSku goodsSku, Map<String, Object> skuInfo) {
        Assert.notNull(goodsSku, "goodsSku不能为空");
        Assert.notEmpty(skuInfo, "skuInfo不能为空");
        //规格简短信息
        StringBuilder simpleSpecs = new StringBuilder();
        //商品名称
        StringBuilder goodsName = new StringBuilder(goodsSku.getGoodsName());
        //规格值
        Map<String, Object> specMap = new LinkedHashMap<>(16);

        // 原始规格项
        String[] ignoreOriginKeys = {"id", "sn", "cost", "price", "quantity", "weight","f","isgreen",
                "greenscore","isCommonweal","isvip","vipPrice","fyVipPrice","isGreenSecurities","isGreenSecuritiesPrice",
                "retailPrice", "isTicket", "isMeal", "mealPrice", "isDefaultMeal","isFree", "productCode", "thirdPartyPlatformType", "thirdPartyPlatformId"};
        //获取规格信息
        for (Map.Entry<String, Object> spec : skuInfo.entrySet()) {
            //保存新增规格信息
            if (!CollUtil.contains(Arrays.asList(ignoreOriginKeys), spec.getKey()) && spec.getValue() != null) {
                specMap.put(spec.getKey(), spec.getValue());
                if (!spec.getKey().equals(GoodsSku.IMAGES_KEY)) {
                    //设置商品名称
                    goodsName.append(" ").append(spec.getValue());
                    //规格简短信息
                    simpleSpecs.append(spec.getValue());
                }
            }
        }
        //设置规格信息
        goodsSku.setGoodsName(goodsName.toString());


        //规格信息
        goodsSku.setId(Convert.toStr(skuInfo.get("id"), ""));
        goodsSku.setSn(Convert.toStr(skuInfo.get("sn")));
        goodsSku.setWeight(Convert.toDouble(skuInfo.get("weight"), 0D));
        goodsSku.setPrice(Convert.toDouble(skuInfo.get("price"), 0D));
        goodsSku.setVipPrice(Convert.toDouble(skuInfo.get("vipPrice"), 0D));
        goodsSku.setFyVipPrice(Convert.toDouble(skuInfo.get("fyVipPrice"), 0D));
        goodsSku.setIsVipGoods(Convert.toBool(skuInfo.get("isvip"), false));
        goodsSku.setCost(Convert.toDouble(skuInfo.get("cost"), 0D));
        goodsSku.setQuantity(Convert.toInt(skuInfo.get("quantity"), 0));
        goodsSku.setSpecs(JSONUtil.toJsonStr(specMap));
        goodsSku.setSimpleSpecs(simpleSpecs.toString());
        goodsSku.setMaxfNumber(Convert.toDouble(skuInfo.get("f"), 0D));
        goodsSku.setIsGreenGoods(Convert.toBool(skuInfo.get("isgreen"), false));
        goodsSku.setIsCommonwealGoods(Convert.toBool(skuInfo.get("isCommonweal"), false));
        goodsSku.setGreenScore(Convert.toDouble(skuInfo.get("greenscore"), 0D));
        goodsSku.setRetailPrice(Convert.toDouble(skuInfo.get("retailPrice"), 0D));
        goodsSku.setIsGreenSecurities(Convert.toBool(skuInfo.get("isGreenSecurities"), false));
        goodsSku.setIsGreenSecuritiesPrice(Convert.toDouble(skuInfo.get("isGreenSecuritiesPrice"), 0D));
        goodsSku.setIsTicket(Convert.toBool(skuInfo.get("isTicket"), false));
        goodsSku.setIsMeal(Convert.toBool(skuInfo.get("isMeal"), false));
        goodsSku.setMealPrice(Convert.toDouble(skuInfo.get("mealPrice"), 0D));
        goodsSku.setIsDefaultMeal(Convert.toBool(skuInfo.get("isDefaultMeal"), false));
        goodsSku.setIsFree(Convert.toBool(skuInfo.get("isFree"), false));
        goodsSku.setThirdPartyPlatformId(Convert.toStr(skuInfo.get("thirdPartyPlatformId"), ""));
        goodsSku.setProductCode(Convert.toStr(skuInfo.get("productCode"), ""));
        //更新商品字段信息
        SceneHelp.objectHandler(goodsSku);
    }

    private static List<GoodsSku> builderBatch(Goods goods, List<Map<String, Object>> skuList) {
        Assert.notEmpty(skuList, "skuList不能为空");
        Assert.notNull(goods, "goods不能为空");
        List<GoodsSku> goodsSkus = new ArrayList<>();
        for (Map<String, Object> skuInfo : skuList) {
            GoodsSku goodsSku = new GoodsSku(goods);
            builderSingle(goodsSku, skuInfo);
            //false 既不是会员商品。把会员价清空
            if (goods.getIsVipGoods() != null && !goods.getIsVipGoods()) {
                goods.setVipPrice(null);
            }
            goodsSkus.add(goodsSku);
        }

        //更新商品字段信息
        goodsSkus.forEach(SceneHelp::objectHandler);

        return goodsSkus;
    }


}
